package micro.data.datasource;


import lombok.Data;
import micro.mybatis.PageInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * @author txw
 */
@Data
public class SesseionFactoryBean {
    private String model;
    private String classPath;

    public SesseionFactoryBean(String model, String classPath) {
        this.model = model;
        this.classPath = classPath;
    }
    public SqlSessionFactory newInstance(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setTypeAliasesPackage(model);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        factoryBean.setMapperLocations(resolver.getResources(classPath));
        SqlSessionFactory factory = factoryBean.getObject();
        factory.getConfiguration().addInterceptor(new PageInterceptor());
        return factory;
    }
}
